using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Xml;
using System.IO;
using NUnit.Framework;

namespace WeatherDataAPI
{
    public class WeatherAPI 
    {      
        static void Main(string[] args)
        {
            WeatherData.city_destination = "Warren";
            WeatherData.state_destination = "MI";
            WeatherData.city_arrival = "Roanoke";
            WeatherData.state_arrival = "VA";
            WeatherData.location_index = 0;

            for (WeatherData.location_index = 1; WeatherData.location_index <= 1; WeatherData.location_index++)
            {
                WeatherData weatherDataDestination = new WeatherData();

                //Output to window console to varify data
                Console.WriteLine("Place:             " + WeatherData.parsePlace);
                Console.WriteLine("Observation Time:  " + WeatherData.obs_time);
                Console.WriteLine("Weather:           " + WeatherData.weather1);
                Console.WriteLine("Temperature:       " + WeatherData.temperature_string);
                Console.WriteLine("Relative Humidity: " + WeatherData.relative_humidity);
                Console.WriteLine("Wind:              " + WeatherData.wind_string);
                Console.WriteLine("Pressure (mb):     " + WeatherData.pressure_mb);
                Console.WriteLine("Dewpoint:          " + WeatherData.dewpoint_string);
                Console.WriteLine("Visibility (km):   " + WeatherData.visibility_km);
                Console.WriteLine("Location:          " + WeatherData.longitude + ", " + WeatherData.latitude);
                Console.WriteLine("Precipitation:     " + WeatherData.precip_today_string);
                Console.WriteLine("Index #:           " + WeatherData.location_index);
                Console.WriteLine("");
            }
            WeatherData weatherDataArrival = new WeatherData();

            //Output to window console to varify data
            Console.WriteLine("Place:             " + WeatherData.parsePlace);
            Console.WriteLine("Observation Time:  " + WeatherData.obs_time);
            Console.WriteLine("Weather:           " + WeatherData.weather1);
            Console.WriteLine("Temperature:       " + WeatherData.temperature_string);
            Console.WriteLine("Relative Humidity: " + WeatherData.relative_humidity);
            Console.WriteLine("Wind:              " + WeatherData.wind_string);
            Console.WriteLine("Pressure (mb):     " + WeatherData.pressure_mb);
            Console.WriteLine("Dewpoint:          " + WeatherData.dewpoint_string);
            Console.WriteLine("Visibility (km):   " + WeatherData.visibility_km);
            Console.WriteLine("Location:          " + WeatherData.longitude + ", " + WeatherData.latitude);
            Console.WriteLine("Precipitation:     " + WeatherData.precip_today_string);
            Console.WriteLine("Index #:           " + WeatherData.location_index);
            Console.WriteLine("");

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }

    class WeatherData 
    {
        string output_xml_destination;
        string output_xml_arrival;

        public static int location_index { get; set; }
        public static string city_destination { get; set; }
        public static string state_destination { get; set; }
        public static string city_arrival { get; set; }
        public static string state_arrival { get; set; }

        public static string place { get; set; }
        public static string obs_time { get; set; }
        public static string weather1 { get; set; }
        public static string temperature_string { get; set; }
        public static string relative_humidity { get; set; }
        public static string wind_string { get; set; }
        public static string pressure_mb { get; set; }
        public static string dewpoint_string { get; set; }
        public static string visibility_km { get; set; }
        public static string longitude { get; set; }
        public static string latitude { get; set; }
        public static string precip_today_string { get; set; }
        public static string parsePlace { get; set; }

        public WeatherData() 
        {
            if (location_index == 1)
            {
                output_xml_destination = "http://api.wunderground.com/api/2a139f540c5eec04/conditions/q/" + city_destination + ", " + state_destination + ".xml";
                string result1 = parse(output_xml_destination);
            }
            else
            {
                output_xml_arrival = "http://api.wunderground.com/api/2a139f540c5eec04/conditions/q/" + city_arrival + ", " + state_arrival + ".xml";
                string result2 = parse(output_xml_arrival);
            }
        }
        public string parse(string input_xml)
        {
            //Declared Variables
            place = "";
            obs_time = "";
            weather1 = "";
            temperature_string = "";
            relative_humidity = "";
            wind_string = "";
            pressure_mb = "";
            dewpoint_string = "";
            visibility_km = "";
            latitude = "";
            longitude = "";
            precip_today_string = "";
            parsePlace = "";

            var cli = new WebClient();
            string weather = cli.DownloadString(input_xml);

            using (XmlReader reader = XmlReader.Create(new StringReader(weather)))
            {
                // Parse the file and display each of the nodes.
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (reader.Name.Equals("full"))
                            {
                                reader.Read();
                                place = reader.Value;
                            }
                            else if (reader.Name.Equals("observation_time"))
                            {
                                reader.Read();
                                obs_time = reader.Value;
                            }
                            else if (reader.Name.Equals("weather"))
                            {
                                reader.Read();
                                weather1 = reader.Value;
                            }
                            else if (reader.Name.Equals("temperature_string"))
                            {
                                reader.Read();
                                temperature_string = reader.Value;
                            }
                            else if (reader.Name.Equals("relative_humidity"))
                            {
                                reader.Read();
                                relative_humidity = reader.Value;
                            }
                            else if (reader.Name.Equals("wind_string"))
                            {
                                reader.Read();
                                wind_string = reader.Value;
                            }
                            else if (reader.Name.Equals("pressure_mb"))
                            {
                                reader.Read();
                                pressure_mb = reader.Value;
                            }
                            else if (reader.Name.Equals("dewpoint_string"))
                            {
                                reader.Read();
                                dewpoint_string = reader.Value;
                            }
                            else if (reader.Name.Equals("visibility_km"))
                            {
                                reader.Read();
                                visibility_km = reader.Value;
                            }
                            else if (reader.Name.Equals("latitude"))
                            {
                                reader.Read();
                                latitude = reader.Value;

                            }
                            else if (reader.Name.Equals("longitude"))
                            {
                                reader.Read();
                                longitude = reader.Value;
                            }
                            else if (reader.Name.Equals("precip_today_string"))
                            {
                                reader.Read();
                                precip_today_string = reader.Value;
                            }
                            break;
                    }
                }               
            }
            
            // This search returns the substring after the first "," of the "place" string.
            // That way the City, State will only be returned.
            string strPlace = place;
            int firstPlace = strPlace.IndexOf(", ") + ", ".Length;
            string str2Place = strPlace.Substring(firstPlace);
            parsePlace = str2Place;
 
            return "";
        }
    }


    [TestFixture]
    public class WeatherNUnitTest
    {
        [Test]
        public void weatherNUnitTest()
        {
            string output_xml_destination;
            string licenseKey = "2a139f540c5eec04";

            output_xml_destination = "http://api.wunderground.com/api/" + licenseKey + "/conditions/q/IL/Chicago.xml";

            WeatherData weatherDataCallTest = new WeatherData();
            string resultTest = weatherDataCallTest.parse(output_xml_destination);
            Assert.AreEqual("West Loop - Old St. Pats, Chicago, Illinois", WeatherData.place);
            Assert.AreEqual("2a139f540c5eec04", licenseKey);
        }
    }
}

